package com.junbao.message.application.service.impl;

import java.util.Date;
import java.util.List;

import com.junbao.message.application.service.MessageService;
import com.junbao.message.domain.model.NotifyMessage;
import com.junbao.message.domain.repository.MessageRepository;
import com.junbao.message.infrastructure.cache.RedisService;
import com.junbao.message.infrastructure.cache.key.MessageKey;
import com.junbao.message.infrastructure.page.DataQuereyCondition;

public class MessageServiceImpl implements MessageService{
	 
    private MessageRepository messageRepository;
 
    
    public void reserveSendMessage(NotifyMessage notifyMessage) {
        messageRepository.saveMessage(notifyMessage);
    }
 
    
    public void confirmSendMessage(NotifyMessage notifyMessage) {
        notifyMessage.sendMessage();
        messageRepository.updateMessage(notifyMessage);
    }
 
    
    public void sendMessage(NotifyMessage notifyMessage) {
        notifyMessage.sendMessage();
        messageRepository.saveMessage(notifyMessage);
    }
 
    
    public void confirmMessage(NotifyMessage notifyMessage) {
        notifyMessage.confirmMessage();
        messageRepository.updateMessage(notifyMessage);
    }
 
    
    public void ackConfirmMessage(NotifyMessage notifyMessage) {
        notifyMessage.markMessageToAck();
        messageRepository.updateMessage(notifyMessage);
    }
 
    
    public void retryMessage(NotifyMessage notifyMessage){
        notifyMessage.sendMessage();
        messageRepository.updateMessage(notifyMessage);
    }
 
    
    public List<NotifyMessage> queryWaitConfirmMessage(Date conditionTime) {
        return messageRepository.queryWaitConfirmMessage(conditionTime);
    }
 
    
    public List<NotifyMessage> queryWaitSendMessage(Date conditionTime) {
        return messageRepository.queryWaitSendMessage(conditionTime);
    }
 
    
    public NotifyMessage queryMessageByMessageSquece(String messageSquece) {
        return messageRepository.queryMessageByMessageSquece(messageSquece);
    }
    
    public NotifyMessage queryMessageByMessageNo(String messageNo) {
    	RedisService redisService = RedisService.getInstance();
    	NotifyMessage cacheMessage = redisService.get(MessageKey.MESSAGE_MESSAGE_NO, messageNo, NotifyMessage.class);
    	if (cacheMessage != null) {
			return cacheMessage;
		}
        NotifyMessage dbMessage = messageRepository.queryMessageByMessageNo(messageNo);
        if (dbMessage != null) {
			redisService.set(MessageKey.MESSAGE_MESSAGE_NO, messageNo, dbMessage);
		}
        return dbMessage;
    }
 
    
    public void updateStatusToWaitConfirm() {
        messageRepository.updateStatusToWaitConfirm();
    }
    
    
    public void updateMessageToDead() {
        messageRepository.updateMessageToDead();
    }
 
    
    public void updateMessageToDead(NotifyMessage message) {
        message.markMessageToDead();
        messageRepository.updateMessageToDead(message);
    }
    
    
    public int queryWaitRemvalTotalCount(DataQuereyCondition condition) {
        return messageRepository.queryWaitRemvalTotalCount(condition);
    }
 
    
    public List<NotifyMessage> queryWaitRemovalMessages(DataQuereyCondition condition) {
        return messageRepository.queryWaitRemovalMessages(condition);
    }
 
    
    public void addWaitMessageToHistory(List<NotifyMessage> notifyMessages) {
        for (NotifyMessage notifyMessage : notifyMessages) {
            messageRepository.addMessageToHistory(notifyMessage);
        }
    }
 
    
    public void deleteRemovaledMessageFromMessage(DataQuereyCondition condition) {
        messageRepository.deleteRemovaledMessageFromMessage(condition);
    }
    
    
    public int updateWaitSendMessageStatusToSending(Date conditionTime){
        return messageRepository.updateWaitSendMessageStatusToSending(conditionTime);
    }
    
    
    public int updateWaitConfirmMessageStatusToConfriming(Date conditionTime) {
        return messageRepository.updateWaitConfirmMessageStatusToConfriming(conditionTime);
    }
    
    @Override
    public void messageStatusToWaitConfirmFromConfirmingUpdate() {
        messageRepository.messageStatusToWaitConfirmFromConfirmingUpdate();
    }
    
    @Override
    public void messageStatusToWaitSendFromSendingUpdate() {
        messageRepository.messageStatusToWaitSendFromSendingUpdate();
    }
    
    public void setMessageRepository(MessageRepository messageRepository) {
        this.messageRepository = messageRepository;
    }    
}
